package com.ruoyi.basis.aspectj.bizLog;

import com.alibaba.fastjson.JSON;
import com.mashuai.utils.base.MsAopUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @ClassName: MsExecuteTimeAop
 * @Description:
 */
@Aspect
@Component
public class MsBizLogAop {


    /**
     * 切入点
     */
    @Pointcut("@annotation(com.ruoyi.basis.aspectj.bizLog.MsBizLog)) ")
    private void pointcut() {
    }

    @Around("pointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        //判断是否开启执行日志打印功能
        MsBizLog logA = MsAopUtils.getAnnotation(joinPoint, MsBizLog.class);
        //默认只开启错误日志，如果全部关闭，直接返回
        if (logA == null || logA.success() || !logA.error()) return joinPoint.proceed();
        Object res = null;

        long bTime = System.currentTimeMillis();
        Throwable exp = null;
        try {
            res = joinPoint.proceed();
        } catch (Throwable e) {
            exp = e;
            e.printStackTrace();
        } finally {
            //错误消息
            String errorMessage = null;
            //类名
            String className = null;
            //方法名
            String methodName = null;
            //方法入参
            String paramsJSON = JSON.toJSONString(MsAopUtils.getMethodParams(joinPoint));
            //错误行数
            Integer lineNumber = null;
            if (exp!=null){
                errorMessage = exp.fillInStackTrace().toString();
                StackTraceElement[] stackTrace = exp.getStackTrace();
                className = stackTrace[0].getClassName();
                methodName = stackTrace[0].getMethodName();
                lineNumber = stackTrace[0].getLineNumber();
                //String fileName = stackTrace[0].getFileName();
            }else {
                className = MsAopUtils.getClassName(joinPoint);
                methodName = MsAopUtils.getMethodName(joinPoint);
            }
            //执行时间
            long exTime = System.currentTimeMillis() - bTime;
            System.err.println("----");
        }
        return res;

    }
}
